//
//  YBCommentVideoView.m
//  ZhongWangYuanApp
//
//  Created by apple on 18/6/4.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YBCommentVideoView.h"
#import "YBStarCommentView.h"

@interface YBCommentVideoView ()<YBStarCommentViewDelegate>
@property (nonatomic , strong) NSMutableArray * itemArray;
@end

@implementation YBCommentVideoView

- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = titleArray;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self initMyView];
    }
    return self;
}


-(void)initMyView{
    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(ItemWidth * 40, ItemHeight * 614, [UIScreen zj_screenWith] - ItemWidth * 80, [UIScreen zj_screenHeight] - ItemHeight * 714)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    
    self.dataArray = [NSMutableArray array];

    for (int i = 0 ; i < self.titleArray.count; i ++) {
        [self.dataArray addObject:@"0"];
        YBStarCommentView * starView = [[YBStarCommentView alloc]initWithFrame:CGRectMake(0, ItemHeight * 21 + ItemHeight * 98 * i, [UIScreen zj_screenWith] - ItemWidth * 80  , ItemHeight * 98) starNumber:5 title:self.titleArray[i]];
        starView.delegate = self;
        starView.tag = 2000 + i;
        [contentView addSubview:starView];
        [self.itemArray addObject:starView];
    }
    
    UIButton * commitButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0, 1, 1) text:@"提交" textColor:[UIColor whiteColor] imageName:@"" font:15];
    commitButton.backgroundColor = [UIColor themeColor];
    [commitButton addTarget:self action:@selector(option_commitButton:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:commitButton];
    
    commitButton.sd_layout.leftSpaceToView(contentView , ItemWidth * 20)
    .rightSpaceToView(contentView , ItemWidth * 20)
    .bottomSpaceToView(contentView , ItemHeight * 20)
    .heightIs(ItemHeight * 88);
    
    UIButton * closeButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0, 1, 1) text:@"" textColor:[UIColor whiteColor] imageName:@"de_ca" font:15];
    [closeButton addTarget:self action:@selector(option_closeButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeButton];
    
    closeButton.sd_layout.rightSpaceToView(self , [UIScreen zj_screenWith] / 2 - ItemWidth * 25)
    .bottomSpaceToView(self , ItemHeight * 25)
    .heightIs(ItemHeight * 50)
    .widthIs(ItemHeight * 50);
    
}

-(void)option_commitButton:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(ybCommentVideoView:didClickCommitButtonWithDataArray: nameArray:)]) {
        [self.delegate ybCommentVideoView:self didClickCommitButtonWithDataArray:[self.dataArray copy] nameArray:self.titleArray];
    }
}


-(void)option_closeButton:(UIButton *)sender{
    [self removeFromSuperview];
}

-(void)StarCommentView:(YBStarCommentView *)starCommentView didSelectedWithCurrentNumber:(NSInteger)currentNumber{
    if ([starCommentView isEqual:self.itemArray[0]]) {
        [self.dataArray removeAllObjects];
        NSString * currentNumberString = [NSString stringWithFormat:@"%ld" , currentNumber];
        for (YBStarCommentView * tempView in self.itemArray) {
            if (![tempView isEqual:starCommentView]) {
                [tempView setStarButtonWithStarNumber:currentNumber];
            }
            [self.dataArray addObject:currentNumberString];
        }
        
    }
    else{
        NSInteger index = starCommentView.tag - 2000;
        [self.dataArray replaceObjectAtIndex:index withObject:[NSString stringWithFormat:@"%ld" , currentNumber]];
    }
}




-(NSMutableArray *)itemArray
{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}




@end
